package com.dp.appi.service.impl;

import com.dp.appi.dao.FootprintDao;
import com.dp.appi.dao.impl.FootprintDaoImpl;
import com.dp.appi.service.ChannelService;
import com.dp.appi.service.FootprintService;
import com.dp.appi.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by bchen on 2016/5/26.
 */

@Service
@Transactional(rollbackFor=Exception.class)
public class FootprintServiceImpl implements FootprintService {
    @Autowired
    private FootprintDao footprintDao;

    /**
     * 按年统计项目数和文章发布数
     *
     * @param user_id
     * @return
     */
    @Override
    public List<Map<String, Object>> stageNumByYear(String user_id) {
        int year = Calendar.getInstance().get(Calendar.YEAR);
        List<Map<String, Object>> footList = new ArrayList<>();
        for(int i=0;i<5;i++){
            Map<String,Object> footMap = footprintDao.stageNumByYear(user_id,year - i);
            footList.add(footMap);
        }
        return footList;
    }

    /**
     * 按年统计项目数和文章发布数
     *
     * @param user_id
     * @param year
     * @return
     */
    @Override
    public Map<String, Map<String, List<Map<String,Object>>>> stageArticleFoot(String user_id, String year) {
        //月份参与项目
//        invol
        List<Map<String, Object>> involList = footprintDao.involList(user_id,year);

        //月份完成项目
        //finish
        List<Map<String, Object>> finishList = footprintDao.finishList(user_id,year);

        //月份发布文章数
        //publish
        List<Map<String, Object>> publishList = footprintDao.publishList(user_id,year);
        Map<String,  Map<String, List<Map<String,Object>>>> sa = new HashMap<>();

        int month = 13;
        if(year.equals(DateUtil.getNowDate("yyyy"))){
            month = Integer.valueOf(DateUtil.getNowDate("MM"));
        }

        for(int i=1;i<=12;i++){
            String ym = year + "-" +i;

            List<Map<String, Object>> saL = new ArrayList<>();
            Map <String, List<Map<String,Object>>> m = new HashMap<>();

            //月份参与项目组装
            List<Map<String, Object>> il = new ArrayList<>();
            if(month>=i) {
                for (Map<String, Object> involMap : involList) {
                    String mon1 = String.valueOf(involMap.get("mon1"));
                    String mon2 = String.valueOf(involMap.get("mon2"));
                    //ym在mon1和mon2之间
                    if (DateUtil.compareStrTo(ym, mon1, "yyyy-MM") == 1 && DateUtil.compareStrTo(ym, mon2, "yyyy-MM") == -1) {
                        il.add(involMap);
                    }
                }
            }
            m.put("invol",il);

            //月份完成项目组装
            List<Map<String, Object>> fl = new ArrayList<>();
            if(month>=i) {
                for (Map<String, Object> finishlMap : finishList) {
                    String mon = String.valueOf(finishlMap.get("mon"));
                    if (DateUtil.compareStrTo(ym, mon, "yyyy-MM") == 0) {
                        fl.add(finishlMap);
                    }
                }
            }
            m.put("finish",fl);

            //月份发布文章数组装
            List<Map<String, Object>> pl = new ArrayList<>();
            if(month>=i) {
                for (Map<String, Object> publishMap : publishList) {
                    String mon = String.valueOf(publishMap.get("mon"));
                    if (DateUtil.compareStrTo(ym, mon, "yyyy-MM") == 0) {
                        pl.add(publishMap);
                    }
                }
            }
            m.put("publish",pl);

            sa.put(String.valueOf(i),  m);
        }
        return sa;
    }

    /**
     * 工作统计
     *
     * @param user_id
     * @return
     */
    @Override
    public Map<String, ?> workStatistics(String user_id) {
        return footprintDao.workStatistics(user_id);
    }
}
